package cn.imu.appdevexperiment2020.cch.cchactivity.fragment;

import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import cn.imu.appdevexperiment2020.R;
import cn.imu.appdevexperiment2020.cch.adapter.ClassAdapter;
import cn.imu.appdevexperiment2020.cch.adapter.LeftClassAdapter;
import cn.imu.appdevexperiment2020.cch.retrofit.api.ApiClient;
import cn.imu.appdevexperiment2020.cch.retrofit.bean.ClassesListResultBean;
import cn.imu.appdevexperiment2020.cch.retrofit.bean.GetByUserNameResultBean;
import cn.imu.appdevexperiment2020.cch.retrofit.bean.ListResultBean;
import cn.imu.appdevexperiment2020.cch.util.AuthUtil;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * 描述:
 *
 * @author chenconghua
 * @version 1.0
 * @create 2022-11-13 18:40
 */
public class CchLeftFragment extends Fragment {
    private static final String TAG = "CchLeftFragment";
    CchMainActivity fragmentMainActivity;
    private Context context;

    @BindView(R.id.left_rec)
    RecyclerView recyclerView;
    @BindView(R.id.left_tv_left_select_text)
    TextView tv_left_select_text;
    private List<ClassesListResultBean.DataBean> list1 =new ArrayList<ClassesListResultBean.DataBean>();
//    private List<GetByUserNameResultBean.DataBean> list2 =new ArrayList<GetByUserNameResultBean.DataBean>();
    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        this.context = context;
        fragmentMainActivity = (CchMainActivity) context;
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_left, container, false);
        ButterKnife.bind(this, view);

        init();
        return view;
    }
    private void init() {
        initData();
    }

    private void initData() {
        Map<String,String> map1=new HashMap<>();
        map1.put("parentId","101");
//      map1.put("")

        Call<ClassesListResultBean> call = ApiClient.getInstance(context.getApplicationContext()).getReturnData2(AuthUtil.GetAuth(),map1);

        call.enqueue(new Callback<ClassesListResultBean>() {
            @Override
            public void onResponse(Call<ClassesListResultBean> call, Response<ClassesListResultBean> response) {
                if (null!=response.body()){

                    if (200==response.body().getCode()){
                        Log.e(TAG,response.body().getMsg());

                        list1.addAll(response.body().getData());
                        initAdapter();
                    }else{

                    }
                }else{

                }
            }


            @Override
            public void onFailure(Call<ClassesListResultBean> call, Throwable t) {

            }
        });

    }
    private void initAdapter() {
        LinearLayoutManager linearLayoutManager=new LinearLayoutManager(context);
        recyclerView.setLayoutManager(linearLayoutManager);
        LeftClassAdapter adapter=new LeftClassAdapter(list1);
        recyclerView.setAdapter(adapter);
    }


}
